﻿var gValidator={
    tipsType:0//0为默认,1为弹窗方式提示
};
//
var rtype = [];//存储正则表达式
//往数组中添加新项的方法
function RtypeAdds(rname, rvalue) {
    rtype.push({
        name: rname,
        val: rvalue
    });
}
//存储需要在点击后执行验证的对象
var vbtn = [];
//添加方法
function BtnAdds(id) {
    vbtn.push({
        id:id
    });
}
//验证插件开始
(function ($) {
    $.Validator = function () {
        //这个是失去焦点时启动验证
        $("[vtype]").blur(function () {
            ValidatorStart($(this));
        });
        //点击按钮时启动验证
        if(vbtn.length>0){
            
            for(var i=0;i<vbtn.length;i++){
                $("#"+vbtn[i].id).bind("click","",function(){
                    
                    var r=[],result=true;    
                    $("[vtype]").each(function(index,vobject){
                       r.push({
                           tf: ValidatorStart(vobject)
                       });
                    });
                    for(var j=0;j<r.length;j++){
                        if(r[j].tf==false){
                            result=false;
                        }
                    }
                    return result;
                });
            }
           
        } 
    };
    //启动验证
    function ValidatorStart(vobject){
        var vjson = eval('(' + $(vobject).attr("vtype") + ')');
            var result = true,vname,tipsid="";
            $.each(vjson, function (v) {
                if(v=='tips'){
                    tipsid=vjson[v][0].id;
                }
                else if (v == 'null') {
                    result = ValidatorNull($(vobject));//验证是否为空
                }
                else if (v == 'number') {
                    result = ValidatorIsNaN($(vobject));//验证是否为数字
                }
                else if (v == 'email') {
                    result = ValidatorEmail($(vobject));//验证是否为电子邮箱
                }
                else if (v == 'tel') {
                    result = ValidatorTel($(vobject));//验证电话号码
                }
                else if (v.toString().indexOf("len") > -1) {
                    result = ValidatorLength($(vobject), v.toString());//验证范围
                }
                else if (v.toString().indexOf("compare-") > -1) {
                    result = ValidatorCompare($(vobject), v.toString());//验证两个值是否相等
                }
                else if (v == "regular") {
                    //正则表达式验证
                    for(var i=0;i<rtype.length;i++){
                        if(rtype[i].name==vjson[v][0].reDbFieldType){
                            result=ValidatorRegExp($(vobject),rtype[i].val);
                        }
                    }
                }
                else if(v=="ajax"){
                    result=ValidatorAjax($(vobject),vjson[v][0].posturl);//Ajax验证
                }
                if(!result){ 
                    Tips($(vobject),"<img src='/style/validator/0.gif' alt=''/>"+vjson[v][0].errormsg,tipsid,false); 
                }
                vname=v;
                return result;
            });
            if(result){
                //Tips($(vobject),vjson[vname][0].bymsg,true);
                Tips($(vobject),"<img src='/style/validator/1.gif' alt=''/>",tipsid,true);
            }
            return result;
        
    }
    //ajax验证(如果验证通过返回1,失败则返回0)
    function ValidatorAjax(vobject,posturl){
        
        var bo=false;
        $.ajax({
            type:'post',
            url:posturl,
            data:''+$(vobject).attr("id")+'='+Trim($(vobject).val()),
            success:function(result){
               if(result=="True"){
                    bo=true;
               }
               else{
                    bo=false;
               }
            },
            async:false
        });
        return bo;
        
    }
    //根据正则验证
    function ValidatorRegExp(vobject, regExp) {
        var r=regExp;
        if (!r.test(Trim($(vobject).val()))) {
            return false;
        }
        else {
            return true;
        }
    }
    //验证两个文本值是否相同
    function ValidatorCompare(vobject, compare) {
        compare = compare.replace("compare-", "");
        if (Trim($(vobject).val()) != Trim($("#" + compare).val())) {
            return false;
        }
        else {
            return true;
        }
    }
    //验证字符长度的范围
    function ValidatorLength(vobject, len) {
        var str = len.replace("len", "").split("-");
        var min = str[0];
        var max = str[1];
        if (Trim($(vobject).val()).length < min || Trim($(vobject).val()).length > max) {
            return false;
        }
        else {
            return true;
        }
    }
    //验证电话号码(如010-123456789)
    function ValidatorTel(vobject) {
        var regExp = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/;
        if (!regExp.test(Trim($(vobject).val()))) {
            return false;
        }
        else {
            return true;
        }
    }
    //验证邮箱
    function ValidatorEmail(vobject) {
        var regExp = /^[A-Za-z\d]+([-_\.\+]*[A-Za-z\d]+)*@(([A-Za-z\d]-?){0,62}[A-Za-z\d]\.)+[A-Za-z\d]{2,6}$/;
        if (!regExp.test(Trim($(vobject).val()))) {
            return false;
        }
        else {
            return true;
        }
    }
    //验证数字
    function ValidatorIsNaN(vobject) {
        if (isNaN(Trim($(vobject).val()))) {
            return false;
        }
        else {
            return true;
        }
    }
    //验证是否为空
    function ValidatorNull(vobject) {
        if (Trim($(vobject).val()) == "") {
            return false;
        } else {
            return true;
        }
    }
    //输出提示信息
    function Tips(vobject,tips,tipsid,tf){
        var id=$(vobject).attr("id");
        var color='red';
        if(tf){
            color='Green';
        }
        
        var html="<span class='validatormsg' style='color:"+color+";font-size:14px'>"+tips+"</span>";
        if(gValidator.tipsType==1){
           
        }else{
            if(tipsid!=""){
                $("#"+tipsid).empty();
                $("#"+tipsid).append(html);
            }else{
                if($("#"+id).next()!=null){
                    $("#"+id).next().empty();
                }
                $("#"+id).after(html);
            }
        }
    }
    function Trim(str)    
    {    
        return str.replace(/(^\s*)|(\s*$)/g, "");    
    }  
})(jQuery);
